package br.poli.ecomp.sma.massim.action;

import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Atom;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.Term;

public class FindDirectionToSurround extends DefaultInternalAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public Object execute(TransitionSystem ts, Unifier un, Term[] args)
			throws Exception {
		
		Term x = args[0];
		Term y = args[1];
		Term newX = args[2];
		Term newY = args[3];
		Term direction = args[4];
		
		double vx = ((NumberTerm)x).solve();
		double vy = ((NumberTerm)y).solve();
		double vNewX = ((NumberTerm)newX).solve();
		double vNewY = ((NumberTerm)newY).solve();
		
		String orientation = "northeast";
		if (vx < vNewX) {
		if (vy < vNewY) {
			orientation = "northeast";
		} else if (vy > vNewY) {
			orientation = "southeast";
		} else {
			orientation = "east";
		}
		} else if (vx > vNewX) {
		if (vy < vNewY) {
			orientation = "northwest";
		} else if (vy > vNewY) {
			orientation = "southwest";
		} else {
			orientation = "west";
		}
		} else {
		if (vy < vNewY) {
			orientation = "north";
		} else if (vy > vNewY) {
			orientation = "south";
		} else {
			orientation = "skip";
		}
		}
	
		if(orientation.equals("north")){
			orientation = "northwest";
		}else if(orientation.equals("northewest") || orientation.equals("northeast")){
			orientation = "north";
		}else if(orientation.equals("west") || orientation.equals("east")){
			orientation = "north";
		}else if(orientation.equals("southwest")){
			orientation = "west";
		}else if(orientation.equals("southeast")){
			orientation = "east";
		}
		
	un.unifies(direction, new Atom(orientation));
	
	// everything ok, so returns true
	return true;
	}
}
